<style rel="stylesheet/scss" lang="scss" scoped>
@import "../../../../styles/prod";

.upload-df {
  vertical-align: top;
  margin-right: 10px;
}

.upload-img {
  padding: 0;
}

.dialog-footer {
  text-align: right;
  padding-top: 20px;
}
</style>
<template>
  <div class="app-container calendar-list-container">
    <div v-if="!productDialog.visible">
      <!-- 基本属性 -->
      <custom-card title="报价单内容填写" class="mt-30">
        <template>
          <basic-form
            :basicInfo="basicForm"
            :ruleForm="detail.pdsFileModelBaseDto"
            ref="basicForm"
            :lang="language"
          ></basic-form>
          <el-button
            type="success"
            size="small"
            @click="addProduct()"
          >添加产品</el-button>
          <el-table :data="detail.productList" border style="width: 100%">
            <el-table-column label="序号" width="60px" align="center">
              <template slot-scope="scope">{{scope.$index+1}}</template>
            </el-table-column>
            <el-table-column prop="pdsFileModelBaseDto.name" label="产品名称" align="center"></el-table-column>
            <el-table-column label="产品规格" align="center">
              <template slot-scope="scope">
                <el-input
                  :disabled="detail.pdsFileModelBaseDto.status ===3 ? true: false"
                  v-model="scope.row.productStandard"
                  placeholder="产品规格"
                  required
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="标准工时" align="center">
              <template slot-scope="scope">
                <el-input
                  :disabled="detail.pdsFileModelBaseDto.status ===3 ? true: false"
                  v-model="scope.row.standardWorkingHours"
                  placeholder="标准工时"
                  required
                  type="number"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="pdsFileModelBaseDto.plmModelNo" label="参考型号" align="center"></el-table-column>
            <el-table-column label="预计订单数量" align="center">
              <template slot-scope="scope">
                <el-input
                  :disabled="detail.pdsFileModelBaseDto.status ===3 ? true: false"
                  v-model="scope.row.estimatedOrderQuantity"
                  placeholder="预计订单数量"
                  required
                  type="number"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="单位" align="center">
              <template slot-scope="scope">
                <el-select
                  :disabled="detail.pdsFileModelBaseDto.status ===3 ? true: false"
                  v-model="scope.row.unitDv"
                  placeholder="单位"
                >
                  <el-option
                    v-for="item in dict.comm_unit"
                    :key="item.id"
                    :label="item.labelDefault"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column prop="pdsFileModelBaseDto.fullCost" label="全成本" align="center"></el-table-column>
            <el-table-column prop="pdsFileModelBaseDto.fobQuote" label="FOB报价" align="center"></el-table-column>
            <el-table-column
              v-if="detail.pdsFileModelBaseDto.status ===3 ? false: true"
              prop="profitRate"
              label="利润率%"
              align="center"
            ></el-table-column>
            <el-table-column v-else label="利润率%" align="center">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.profitRate"
                  placeholder="利润率%"
                  required
                  type="number"
                  @change="priceChange(scope.row.profitRate,scope.$index)"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column
              v-if="detail.pdsFileModelBaseDto.status ===3 ? false: true"
              prop="realQuote"
              label="实际报价"
              align="center"
            ></el-table-column>
            <el-table-column v-else label="实际报价" align="center">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.realQuote"
                  placeholder="实际报价"
                  required
                  type="number"
                  @change="quoteChange(scope.row.realQuote,scope.$index)"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="250px" fixed="right" align="center">
              <template slot-scope="scope">
                <el-button-group>
                  <el-button
                    class="filter-item"
                    type="primary"
                    @click="addProduct(scope.$index)"
                    size="mini"
                  >产品配置</el-button>
                  <el-button
                    class="filter-item"
                    type="info"
                    @click="showHistoryList(scope.row.pdsFileModelBaseDto.plmModelNo)"
                    size="mini"
                  >历史报价</el-button>
                  <!-- <el-button class="filter-item" type="success" @click="ajustPrice(scope.$index)" size="mini">价格调整
                  </el-button>-->
                  <el-button
                    class="filter-item"
                    type="danger"
                    @click="deleteProduct(scope.$index)"
                    size="mini"
                  >删除</el-button>
                </el-button-group>
              </template>
            </el-table-column>
          </el-table>
          <custom-card title="报价条件">
            <!-- <el-button type="success" size="small" @click="addPrice()">添加报价条件</el-button>
            <el-table :data="detail.priceCondition" border style="width: 100%">
              <el-table-column label="序号" width="60px" align="center">
                <template slot-scope="scope">
                  {{scope.$index+1}}
                </template>
              </el-table-column>
              <el-table-column label="条件项" width="200px" align="center">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.condition" placeholder="请选择">
                    <el-option v-for="item in conditionList" :key="item.id"
                               :label="language=='en'?item.labelEnUs:item.labelZhCh" :value="item.id">
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="条件内容" align="center">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.content" placeholder="请输入条件内容" required></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="" label="操作" width="120px" align="center">
                <template slot-scope="scope">
                  <el-button class="filter-item" type="danger" @click="deletePrice(scope.$index)" size="mini">删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>-->
            <el-row class>
              <el-col v-if="detail.pdsFileModelBaseDto.status != 3" :span="6">
                <el-input placeholder="输入关键字进行过滤" v-model="data.asset"></el-input>
                <el-tree
                  class="filter-tree"
                  :data="data.assetTreeList"
                  default-expand-all
                  @node-click="handleTreeClick"
                  :filter-node-method="filterAsset"
                  ref="treeAsset"
                ></el-tree>
              </el-col>
              <el-col
                :span="detail.pdsFileModelBaseDto.status ===3 ? 24: 18"
                style="padding-left:20px"
              >
                <el-table :data="data.showAccessoriesList" border style="width: 100%">
                  <el-table-column label="序号" width="60px" type="index" align="center">
                    <template slot-scope="scope">{{scope.$index+1}}</template>
                  </el-table-column>
                  <el-table-column label="条件项" width>
                    <template
                      slot-scope="scope"
                    >{{language=='en'?scope.row.nameEn:scope.row.nameCn}}</template>
                  </el-table-column>
                  <el-table-column label="条件内容" width="200px" align="center">
                    <template slot-scope="scope">
                      <el-input
                        :disabled="detail.pdsFileModelBaseDto.status ===3 ? true: false"
                        v-model="scope.row.conditionContext"
                      ></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column
                    v-if="detail.pdsFileModelBaseDto.status != 3"
                    label="操作"
                    width="100px"
                    align="center"
                  >
                    <template slot-scope="scope">
                      <el-button
                        class="filter-item"
                        type="danger"
                        size="mini"
                        @click="deletItem(scope.$index,data.showAccessoriesList)"
                      >删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
            </el-row>
          </custom-card>
          <custom-card title="其他" class="mt-30">
            <template>
              <basic-form
                :basicInfo="controlForm"
                :ruleForm="detail.pdsQuoteOther"
                ref="basicForm"
                :lang="language"
              ></basic-form>
              <el-checkbox
                :disabled="detail.pdsFileModelBaseDto.status ===3 ? true: false"
                v-model="detail.pdsQuoteOther.declaration"
              >
                The above price is net price and do not include any commssion,third
                party test cost,inspection cost etc.
              </el-checkbox>
            </template>
          </custom-card>
        </template>
      </custom-card>
      <div class="pds-btm mt-30">
        <div v-if="detail.pdsFileModelBaseDto.status == 0">
          已保存草稿
          <span class="tip">{{detail.pdsFileModelBaseDto.updateTime | formatTime}}</span>
        </div>
        <div class="right">
          <el-button
            class="filter-item"
            type="warning"
            @click="submitForm('draft')"
            :loading="loading"
          >保存草稿</el-button>
          <el-button
            class="filter-item"
            type="primary"
            @click="submitForm('save')"
            :loading="loading"
          >提交</el-button>
        </div>
      </div>
    </div>
    <!-- 添加产品 -->
    <div v-show="productDialog.visible">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="选择参考产品型号"
        v-model="search.context"
      ></el-input>
      <el-button
        class="filter-item"
        type="primary"
        icon="search"
        @click="importModel()"
        size="small"
      >查询</el-button>
      <prod-file ref="prodfile" page="price" v-show="productDialog.product.pdsFileModelBaseDto"></prod-file>
      <div class="dialog-footer">
        <el-button @click="productDialog.visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="chooseProduct()" size="small">确 定</el-button>
      </div>
    </div>
    <el-dialog title="历史数据" :visible.sync="centerDialogVisible" width="30%" center>
      <el-form :inline="true" class="filter">
        <el-form-item v-if="historyQuote.length === 0">
          <span>暂无数据</span>
        </el-form-item>
        <el-form-item
          v-else
          v-for="(item, index) in historyQuote"
          :key="index"
          :label="`历史报价${index+1}：`"
        >
          <span>{{item}}</span>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import BasicForm from "@/components/BasicForm/index";
import * as baseApi from "@/api/common/index";
import * as addApi from "@/api/product/model/add";
import * as priceApi from "@/api/sale/price/index";
import ProdFile from "./prodFile";

export default {
  name: "newQuotation",
  components: {
    BasicForm,
    ProdFile
    // 'depart-selector': () =>
    //   import ('./components/departSelector')
  },
  data() {
    return {
      productDialog: {
        visible: false,
        index: null,
        product: {}
      },
      data: {
        assetTreeList: [],
        showAccessoriesList: []
      },
      historyQuote: [],
      centerDialogVisible: false,
      loading: false,
      search: {
        importType: 2
      },
      detail: {
        pdsFileModelBaseDto: {},
        pdsQuoteOther: {
          declaration: false
        },
        productList: [], // 添加产品
        priceCondition: [] // 报价条件
      },
      conditionList: [
        {
          id: 1,
          labelZhCh: "交易条件"
        },
        {
          id: 2,
          labelZhCh: "交货期"
        },
        {
          id: 3,
          labelZhCh: "最小起订量"
        },
        {
          id: 4,
          labelZhCh: "装箱方式"
        },
        {
          id: 5,
          labelZhCh: "付款条件"
        },
        {
          id: 6,
          labelZhCh: "标准包装方式"
        },
        {
          id: 7,
          labelZhCh: "价格有效期"
        },
        {
          id: 8,
          labelZhCh: "影响价格因素"
        }
      ],
      controlForm: [
        {
          label: "sale.price.returnsRate",
          unit: "",
          element: "input",
          name: "returnsRate",
          type: "",
          col: 100,
          isRequired: false
        },
        {
          label: "sale.price.shelfLife",
          unit: "",
          element: "input",
          name: "shelfLife",
          type: "",
          col: 100,
          isRequired: false
        }
      ],
      editControlForm: [
        {
          label: "sale.price.returnsRate",
          unit: "",
          element: "input",
          name: "returnsRate",
          type: "",
          col: 100,
          disabled: true,
          isRequired: false
        },
        {
          label: "sale.price.shelfLife",
          unit: "",
          element: "input",
          name: "shelfLife",
          type: "",
          col: 100,
          disabled: true,
          isRequired: false
        }
      ],
   basicForm: [
        {
          label: "sale.price.quoteNo",
          unit: "",
          element: "input",
          name: "quoteNo",
          type: "",
          disabled: true,
          isRequired: false,
          placeholder: " "
        },
        {
          label: "sale.price.quoteTime",
          unit: "",
          element: "datePicker",
          name: "quoteTime",
          type: "date",
          isRequired: true
        },
        {
          label: "sale.price.invalidTime",
          unit: "",
          element: "datePicker",
          name: "invalidTime",
          type: "date",
          isRequired: true
        },
        {
          label: "sale.price.customer",
          unit: "",
          element: "selectFilter",
          name: "customerId",
          type: "filter",
          isRequired: true
        },
        {
          label: "sale.price.customerName",
          unit: "",
          name: "customerName",
          element: "input",
          type: "",
          disabled: true,
          isRequired: false,
          options: [],
          placeholder: " "
        },
        {
          label: "sale.price.customerModel",
          unit: "",
          element: "input",
          name: "customerModel",
          type: "",
          isRequired: true,
          placeholder: " "
        },
        {
          label: "sale.price.adjustPriceCondition",
          unit: "",
          element: "input",
          name: "adjustPriceConditionDv",
          type: "",
          dict: "pds_customer_adjust",
          isRequired: true,
          disabled: true,
          placeholder: " "
        },
        {
          label: "sale.price.quoteType",
          unit: "",
          element: "select",
          dict: "pds_customer_offer_type",
          name: "quoteTypeDv",
          append: "",
          type: "",
          isRequired: true
        },
        {
          label: "sale.price.currency",
          unit: "",
          element: "select",
          name: "currencyDv",
          type: "",
          dict: "comm_currency",
          isRequired: true
        },
        {
          label: "sale.price.taxRate",
          unit: "",
          element: "select",
          name: "taxRateDv",
          append: "",
          dict: "pds_customer_tmp_tax",
          type: "",
          isRequired: true
        },
        {
          label: "sale.price.paymentTerms",
          unit: "",
          element: "select",
          name: "paymentTermsDv",
          type: "",
          dict: "pds_customer_payment",
          isRequired: true
        },
        {
          label: "sale.price.transportationType",
          unit: "",
          element: "select",
          name: "transportationTypeDv",
          type: "",
          dict: "pds_customer_transport",
          isRequired: true
        },
        {
          label: "sale.price.portShipment",
          unit: "",
          element: "select",
          append: "",
          name: "portShipmentDv",
          type: "",
          dict: "pds_customer_tmp_port",
          isRequired: false
        },
        {
          label: "sale.price.remark",
          unit: "",
          name: "remark",
          element: "textarea",
          type: "",
          isRequired: false
        }
      ],
  editBasicForm: [
        {
          label: "sale.price.quoteNo",
          unit: "",
          element: "input",
          name: "quoteNo",
          type: "",
          disabled: true,
          isRequired: false,
          placeholder: " "
        },
        {
          label: "sale.price.quoteTime",
          unit: "",
          element: "input",
          name: "quoteTime",
          type: "",
          disabled: true,
          isRequired: true
        },
        {
          label: "sale.price.invalidTime",
          unit: "",
          element: "input",
          name: "invalidTime",
          type: "",
          disabled: true,
          isRequired: true
        },
        {
          label: "sale.price.customer",
          unit: "",
          element: "selectFilter",
          name: "customerId",
          type: "filter",
          disabled: true,
          isRequired: true
        },
        {
          label: "sale.price.customerName",
          unit: "",
          name: "customerName",
          element: "input",
          type: "",
          disabled: true,
          isRequired: false,
          options: [],
          placeholder: " "
        },
        {
          label: "sale.price.customerModel",
          unit: "",
          element: "input",
          name: "customerModel",
          type: "",
          disabled: true,
          isRequired: true,
          placeholder: " "
        },
        {
          label: "sale.price.adjustPriceCondition",
          unit: "",
          element: "input",
          name: "adjustPriceConditionDv",
          type: "",
          dict: "pds_customer_adjust",
          isRequired: true,
          disabled: true,
          placeholder: " "
        },
        {
          label: "sale.price.quoteType",
          unit: "",
          element: "select",
          dict: "pds_customer_offer_type",
          name: "quoteTypeDv",
          append: "",
          type: "",
          disabled: true,
          isRequired: true
        },
        {
          label: "sale.price.currency",
          unit: "",
          element: "select",
          name: "currencyDv",
          type: "",
          disabled: true,
          dict: "comm_currency",
          isRequired: true
        },
        {
          label: "sale.price.taxRate",
          unit: "",
          element: "select",
          name: "taxRateDv",
          append: "",
          dict: "pds_customer_tmp_tax",
          type: "",
          disabled: true,
          isRequired: true
        },
        {
          label: "sale.price.paymentTerms",
          unit: "",
          element: "select",
          name: "paymentTermsDv",
          type: "",
          disabled: true,
          dict: "pds_customer_payment",
          isRequired: true
        },
        {
          label: "sale.price.transportationType",
          unit: "",
          element: "select",
          name: "transportationTypeDv",
          type: "",
          disabled: true,
          dict: "pds_customer_transport",
          isRequired: true
        },
        {
          label: "sale.price.portShipment",
          unit: "",
          element: "select",
          append: "",
          name: "portShipmentDv",
          type: "",
          disabled: true,
          dict: "pds_customer_tmp_port",
          isRequired: false
        },
        {
          label: "sale.price.remark",
          unit: "",
          name: "remark",
          element: "textarea",
          type: "",
          disabled: true,
          isRequired: false
        }
      ],
      productCategory: []
    };
  },
  created() {
    this.getDict();
    this.dataInit();
    this.getPdsConfAssetTree(3);
    this.refresh();
  },
  computed: {
    ...mapGetters(["elements", "language", "dict"])
  },
  methods: {
    /* 页面刷新初始化 */
    refresh() {
      if (this.$route.query.quoteId) {
        /* 编辑 */
        this.getDetail();
      }
    },
    clearValidate() {
      setTimeout(() => {
        this.$refs.basicForm &&
          this.$refs.basicForm.$refs.ruleForm.clearValidate();
      });
    },
    refreshDetail(res) {
      this.dataInit();
      this.clearValidate();
      if (res.code == 200) {
        console.log('xxx', this.data.assetTreeList)
        this.$set(this.detail, "pdsFileModelBaseDto", res.data);
        this.$set(this.detail, "pdsQuoteOther", res.data.pdsQuoteOther);
        this.$set(
          this.data,
          "showAccessoriesList",
          res.data.pdsQuoteConditionList
        );
        if (this.detail.pdsFileModelBaseDto.status === 3) {
          this.basicForm = JSON.parse(JSON.stringify(this.editBasicForm));
          this.controlForm = JSON.parse(JSON.stringify(this.editControlForm));
        }
        const pdsProductConfigList = [];
        let basePro = {};
        let baseMou = {};
        let paramList = {};
        let resData = res.data.pdsProductConfigList;
        resData.map(itemPro => {
          basePro = {
            pdsFileModelBaseDto: {}
          };
          basePro.pdsFileModelBaseDto.plmModelNo = itemPro.plmModelNo;
          basePro.pdsFileModelBaseDto.productCategoryId = itemPro.productCategoryId;
          basePro.pdsFileModelBaseDto.pdsArticleNo = itemPro.plmArticleNo;
          basePro.standardWorkingHours = itemPro.standardWorkingHours;
          basePro.pdsFileModelBaseDto.name = itemPro.productName;
          basePro.pdsFileModelBaseDto.fullCost = itemPro.fullCost;
          basePro.pdsFileModelBaseDto.fobQuote = itemPro.fobQuote;
          basePro.productStandard = itemPro.productStandard;
          basePro.estimatedOrderQuantity = itemPro.estimatedOrderQuantity;
          basePro.unitDv = itemPro.unitDv;
          basePro.id = itemPro.id;
          basePro.profitRate = itemPro.profitRate;
          basePro.realQuote = itemPro.realQuote;
          basePro.showModules = [];
          if (itemPro.productModuleList.length > 0) {
            itemPro.productModuleList.map(itmMou => {
              baseMou = {};
              baseMou.id = itmMou.id;
              baseMou.moduleId = itmMou.moduleId;
              baseMou.itemCode = itmMou.itemCode;
              baseMou.itemNameCn = itmMou.itemNameCn;
              baseMou.itemNameEn = itmMou.itemNameEn;
              baseMou.paramsList = [];
              if (itmMou.paramsList.length > 0) {
                itmMou.paramsList.map(param => {
                  paramList = {};
                  paramList.id = param.id;
                  paramList.paramValue = param.paramValue;
                  paramList.paramMaxValue = param.paramMaxValue;
                  paramList.paramKey = param.paramKey;
                  paramList.paramCode = param.paramCode;
                  paramList.paramNameCn = param.paramNameCn;
                  paramList.paramNameEn = param.paramNameEn;
                  paramList.paramPromptWord = param.paramPromptWord;
                  paramList.paramRequired = param.paramRequired;
                  paramList.paramTypeDv = param.paramTypeDv;
                  paramList.paramUnitDv = param.paramUnitDv;
                  paramList.standard = param.standard;
                  baseMou.paramsList.push(paramList);
                });
              }
              basePro.showModules.push(baseMou);
            });
          }
          this.detail.productList.push(basePro);
        });
      }
    },

    getDetail() {
      priceApi.getDetail({ quoteId: this.$route.query.quoteId }).then(res => {
        this.refreshDetail(res);
        // if (res.code == 200) {
        //   this.$set(this.detail, "pdsFileModelBaseDto", res.data);
        //   this.$set(this.detail, "pdsQuoteOther", res.data.pdsQuoteOther);
        //   this.$set(
        //     this.data,
        //     "showAccessoriesList",
        //     res.data.pdsQuoteConditionList
        //   );
        //   if (this.detail.pdsFileModelBaseDto.status === 3) {
        //     this.basicForm = JSON.parse(JSON.stringify(this.editBasicForm));
        //     this.controlForm = JSON.parse(JSON.stringify(this.editControlForm));
        //   }
        //   const pdsProductConfigList = [];
        //   let basePro = {};
        //   let baseMou = {};
        //   let paramList = {};
        //   let resData = res.data.pdsProductConfigList;
        //   resData.map(itemPro => {
        //     basePro = {
        //       pdsFileModelBaseDto: {}
        //     };
        //     basePro.pdsFileModelBaseDto.plmModelNo = itemPro.plmModelNo;
        //     basePro.pdsFileModelBaseDto.productCategoryId =
        //       itemPro.productCategoryId;
        //     basePro.standardWorkingHours = itemPro.standardWorkingHours;
        //     basePro.pdsFileModelBaseDto.name = itemPro.productName;
        //     basePro.pdsFileModelBaseDto.fullCost = itemPro.fullCost;
        //     basePro.pdsFileModelBaseDto.fobQuote = itemPro.fobQuote;
        //     basePro.productStandard = itemPro.productStandard;
        //     basePro.estimatedOrderQuantity = itemPro.estimatedOrderQuantity;
        //     basePro.unitDv = itemPro.unitDv;
        //     basePro.id = itemPro.id;
        //     basePro.profitRate = itemPro.profitRate;
        //     basePro.realQuote = itemPro.realQuote;
        //     basePro.showModules = [];
        //     if (itemPro.productModuleList.length > 1) {
        //       itemPro.productModuleList.map(itmMou => {
        //         baseMou = {};
        //         baseMou.id = itmMou.id;
        //         baseMou.moduleId = itmMou.moduleId;
        //         baseMou.itemCode = itmMou.itemCode;
        //         baseMou.paramsList = [];
        //         if (itmMou.paramsList.length > 1) {
        //           itmMou.paramsList.map(param => {
        //             paramList = {};
        //             paramList.id = param.id;
        //             paramList.paramValue = param.paramValue;
        //             paramList.paramMaxValue = param.paramMaxValue;
        //             paramList.paramKey = param.paramKey;
        //             paramList.paramCode = param.paramCode;
        //             paramList.paramNameCn = param.paramNameCn;
        //             paramList.paramNameEn = param.paramNameEn;
        //             paramList.paramPromptWord = param.paramPromptWord;
        //             paramList.paramRequired = param.paramRequired;
        //             paramList.paramTypeDv = param.paramTypeDv;
        //             paramList.paramUnitDv = param.paramUnitDv;
        //             paramList.standard = param.standard;
        //             baseMou.paramsList.push(paramList);
        //           });
        //         }
        //         basePro.showModules.push(baseMou);
        //       });
        //     }
        //     this.detail.productList.push(basePro);
        //   });
        //   //this.$set(this.detail, 'productList',  this.deatal.productList);
        //   // console.log(this.detail.productList)
        //   // return
        //   // const setMulSelect = str => {
        //   //   let arr = str.split(',');
        //   //   arr = arr.map(row => Number(row));
        //   //   return arr;
        //   // };
        //   // console.log('customerIds',res.data.customerIds)
        //   // this.$set(this.pdsFileModelBaseDto, 'customerIds', setMulSelect(res.data.customerIds));
        //   // /* 客户代码回显 */
        //   // let customerNameArr = [];
        //   // if (res.data.customerNames) res.data.customerNames.split(',');
        //   // customerNameArr.map((item, index) => {
        //   //   this.$refs.basicForm.getPdsCustomerByName(item);
        //   // })

        //   // this.$set(this.pdsFileModelBaseDto, 'pdsConfCategory', setMulSelect(res.data.productCategoryIds));
        //   // this.$set(this.pdsFileModelBaseDto, 'certsDvs', setMulSelect(res.data.certsDvs));
        //   // // this.$set(
        //   // //   this.pdsFileModelBaseDto,
        //   // //   'pdsFileModelPbsPicsList',
        //   // //   res.data.pdsPlanningModelPicsList
        //   // // );
        //   // console.log('detail', this.pdsFileModelBaseDto);
        // }
      });
    },
    dataInit() {
      // this.data = {
      //   assetTreeList: [],
      //   showAccessoriesList: []
      // };
      this.detail = {
        pdsFileModelBaseDto: {},
        pdsQuoteOther: {
          declaration: false
        },
        productList: [], // 添加产品
        priceCondition: [] // 报价条件
      };
    },
    // 筛选附件
    filterAsset(value, data) {
      if (!value) return true;
      if (this.language === "en") {
        return data.nameEn.indexOf(value) !== -1;
      } else {
        return data.nameCn.indexOf(value) !== -1;
      }
    },
    // 获取附件树
    getPdsConfAssetTree() {
      priceApi
        .getPdsSaleConfAssetTree({
          type: 1
        })
        .then(res => {
          this.data.assetTreeList = res.data;
        });
    },
    // 是否是新增附件选项
    handleTreeClick(data) {
      let isNew = true;
      console.log(this.language == "en");
      this.data.showAccessoriesList.map(row => {
        if (row.assetId === data.id) {
          isNew = false;
        }
        // return row
      });
      if (isNew) {
        data.assetId = data.id;
        data.nameCn = data.nameCn;
        data.conditionContext = "";
        this.data.showAccessoriesList.push({
          assetId: data.id,
          id: data.id,
          nameCn: data.nameCn,
          nameEn: data.nameEn,
          conditionContext: data.conditionContext
        });
      }
    },

    quoteChange(val, index) {
      priceApi
        .adjustPrice({
          configId: this.detail.pdsFileModelBaseDto.pdsProductConfigList[index]
            .id,
          type: 2,
          input: val
        })
        .then(res => {
          this.loading = false;
          if (res.code === 200) {
            this.$message.info("保存成功");
            console.log("asdsadas", this.detail.productList[index]);
            this.detail.productList[index].profitRate = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    priceChange(val, index) {
      priceApi
        .adjustPrice({
          configId: this.detail.pdsFileModelBaseDto.pdsProductConfigList[index]
            .id,
          type: 1,
          input: val
        })
        .then(res => {
          this.loading = false;
          if (res.code === 200) {
            this.$message.info("保存成功");
            console.log("asdsadas", this.detail.productList[index]);
            this.$set(this.detail.productList[index], "realQuote", res.data);
            // this.detail.productList[index].realQuote = res.data
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 删除特征或者附件
    deletItem(index, list) {
      list.splice(index, 1);
    },
    addProduct(index) {
      this.productDialog.visible = true;
      if (index > -1) {
        this.productDialog.index = index;
        this.productDialog.product = JSON.parse(
          JSON.stringify(this.detail.productList[index])
        );
        this.search.context = this.productDialog.product.pdsFileModelBaseDto.plmModelNo;
      } else {
        this.productDialog.product = {};
        this.search.context = "";
      }
      this.$refs.prodfile.refresh(this.productDialog.product);
    },
    async chooseProduct() {
      if(!this.productDialog.product.showModules){
        return;
      }
      const moduleFormRes = await this.$refs.prodfile.submitForm();
      if (!moduleFormRes.isValid) {
        this.$message({
          type: "info",
          message: "请填写完整信息"
        });
        return;
      }
      this.detail.showModules = moduleFormRes.data.showModules;
      const updataParams = JSON.parse(JSON.stringify(this.detail));
      const setModuleData = () => {
        updataParams.showModules.map(moduleItem => {
          if (moduleItem.paramsList && moduleItem.paramsList.length) {
            moduleItem.paramsList.map(paramsItem => {
              paramsItem.paramValue = moduleItem.formData[paramsItem.id];
              paramsItem.paramMaxValue =
                moduleItem.formData[paramsItem.id + "m"];
            });
          }
        });
      };
      setModuleData();
      // console.log(this.productDialog.product)
      // console.log('moduleFormRes',updataParams.showModules)
      const index = this.productDialog.index;
      this.productDialog.product.showModules = updataParams.showModules;
      if (index != null) {
        this.detail.productList[index] = JSON.parse(
          JSON.stringify(this.productDialog.product)
        );
      } else {
        this.detail.productList.push(
          JSON.parse(JSON.stringify(this.productDialog.product))
        );
      }
      this.search.context = "";
      this.productDialog = {
        visible: false,
        index: null,
        product: {}
      };
    },
    importModel() {
      addApi.importOldModel(this.search).then(res => {
        if (res.code === 200) {
          this.productDialog.product = res.data;
          this.$refs.prodfile.refresh(res.data);
        } else {
          this.$message.error(res.message);
        }
        // this.detail.pdsFileModelBaseDto.certsDvs=[]
      });
    },

    deleteProduct(index) {
      this.detail.productList.splice(index, 1);
    },
    deletePrice(index) {
      this.detail.priceCondition.splice(index, 1);
    },
    addPrice() {
      this.detail.priceCondition.push({});
    },
    // 获取字典选项
    getDict() {
      let codes = [];
      this.basicForm.map(item => {
        if (item.dict) {
          codes.push(item.dict);
        }
      });
      codes = codes.join(",");

      baseApi.getTypesValue(codes).then(res => {
        this.basicForm.forEach(value => {
          if (value.dict) {
            this.$set(value, "options", res.data[value.dict]);
          }
        });
      });
    },
    // 查看历史报价
    showHistoryList(id) {
      this.centerDialogVisible = true;
      priceApi.getHistoryQuote({ plmModelNo: id }).then(res => {
        this.historyQuote = res.data;
      });
    },
    // 提交或草稿
    async submitForm(type) {
      const basicFormCheck = await this.$refs.basicForm.submitForm();
      const moduleFormRes = await this.$refs.prodfile.submitForm();
      if (!basicFormCheck) {
        this.$message.error("请填写完整基本信息");
        return;
      }
      const pdsProductConfigList = [];
      let basePro = {};
      let baseMou = {};
      let paramList = {};
      let updataParams = {
        ...this.detail.pdsFileModelBaseDto,
        pdsProductConfigList: [],
        pdsQuoteOther: { ...this.detail.pdsQuoteOther },
        pdsQuoteConditionList: this.data.showAccessoriesList,
        quoteType: 0
      };
      if (type === "save") {
        // 提交
        updataParams.operation = "submit";
      } else {
        // 草稿
        updataParams.operation = "save";
      }
      this.detail.productList.map(itemPro => {
        basePro = {};
        basePro.plmModelNo = itemPro.pdsFileModelBaseDto.plmModelNo;
        basePro.productCategoryId = itemPro.pdsFileModelBaseDto.productCategoryId;
        basePro.plmArticleNo = itemPro.pdsFileModelBaseDto.pdsArticleNo
        basePro.standardWorkingHours = itemPro.standardWorkingHours;
        basePro.profitRate = itemPro.profitRate;
        basePro.realQuote = itemPro.realQuote;
        basePro.fullCost = itemPro.pdsFileModelBaseDto.fullCost 
        basePro.productName = itemPro.pdsFileModelBaseDto.name;
        basePro.productStandard = itemPro.productStandard;
        basePro.estimatedOrderQuantity = itemPro.estimatedOrderQuantity;
        basePro.unitDv = itemPro.unitDv;
        basePro.id = itemPro.id || "";
        basePro.productModuleList = [];
        if (itemPro.showModules.length > 0) {
          itemPro.showModules.map(itmMou => {
            baseMou = {};
            baseMou.id = itmMou.id;
            baseMou.moduleId = itmMou.moduleId;
            baseMou.itemCode = itmMou.itemCode;
            baseMou.paramsList = [];
            if (itmMou.paramsList.length > 0) {
              itmMou.paramsList.map(param => {
                paramList = {};
                paramList.id = param.id;
                paramList.paramValue = param.paramValue;
                paramList.paramMaxValue = param.paramMaxValue;
                baseMou.paramsList.push(paramList);
              });
            }
            basePro.productModuleList.push(baseMou);
          });
        }
        updataParams.pdsProductConfigList.push(basePro);
      });
      this.saveToServer(type, updataParams);

    },
    saveToServer(type, updataParams) {
      if (this.loading) return;
      this.loading = true;
      let api = priceApi;
      if (this.detail.pdsFileModelBaseDto.status === 3) {
        priceApi.adjustSave(updataParams).then(res => {
          this.loading = false;
          if (res.code === 200) {
            this.$message.info("保存成功");
            this.refreshDetail(res);
            if (type === "save") {
              this.$store.dispatch("delActiveViews");
              //this.$router.go(-1);
            }
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        priceApi.save(updataParams).then(res => {
          this.loading = false;
          if (res.code === 200) {
            this.$message.info("保存成功");
            this.refreshDetail(res);
            if (type === "save") {
              this.$store.dispatch("delActiveViews");
            }
          } else {
            this.$message.error(res.message);
          }
        });
      }
    }
  }
};
</script>
